import 'dart:convert';

import 'package:dio/dio.dart';

import 'osc_notice_v2.dart';

class OscBaseListEntity<T> {
	OscBaseListResult<T> result;
	List<T> listResult;
	int code;
	String time;
	String message;
	OscNoticeV2 notice;
	bool success = false;

	OscBaseListEntity.fromParams({this.result, this.code, this.time, this.message, this.notice, this.success});

	factory OscBaseListEntity(jsonStr, Function buildFun){
		if(jsonStr != null) {
			if(jsonStr is Response) {
				var data = jsonStr.data;
				return data is String ? OscBaseListEntity.fromJson(json.decode(data), buildFun) : OscBaseListEntity.fromJson(data, buildFun);
			} else {
				return jsonStr is String ? OscBaseListEntity.fromJson(json.decode(jsonStr), buildFun) : OscBaseListEntity.fromJson(jsonStr, buildFun);
			}
		} else {
			return OscBaseListEntity.fromParams(code: 9999, message: "request no response", success: false);
		}
	}

	OscBaseListEntity.fromJson(Map<String, dynamic> json, Function buildFun) {
		code = json['code'];
		time = json['time'];
		message = json['message'];
		notice = json['notice'] != null ? new OscNoticeV2.fromJson(json['notice']) : null;

		if(code == 1){
			success = true;
		}
		if(json['result'] != null && buildFun != null) {
			if(json['result'] is List){
				listResult = new List<T>();
				(json['result'] as List).forEach((v) { listResult.add(buildFun(v)); });
			} else {
				result = OscBaseListResult.fromJson(json['result'], buildFun);
			}
		}
	}

	Map<String, dynamic> toJson() {
		final Map<String, dynamic> data = new Map<String, dynamic>();
		if (this.result != null) {
      data['result'] = this.result.toJson();
    }
		data['code'] = this.code;
		data['time'] = this.time;
		data['message'] = this.message;
		if (this.notice != null) {
      data['notice'] = this.notice.toJson();
    }
		return data;
	}
}

class OscBaseListResult<T> {
	String prevPageToken;
	int requestCount;
	int totalResults;
	String nextPageToken;
	int responseCount;
	List<T> items;

	OscBaseListResult({this.prevPageToken, this.requestCount, this.totalResults, this.nextPageToken, this.responseCount, this.items});

	OscBaseListResult.fromJson(Map<String, dynamic> json, Function buildFun) {
		prevPageToken = json['prevPageToken'];
		requestCount = json['requestCount'];
		totalResults = json['totalResults'];
		nextPageToken = json['nextPageToken'];
		responseCount = json['responseCount'];
		if (json['items'] != null && buildFun != null) {
			items = new List<T>();
			(json['items'] as List).forEach((v) { items.add(buildFun(v)); });
		}
	}

	Map<String, dynamic> toJson() {
		final Map<String, dynamic> data = new Map<String, dynamic>();
		data['prevPageToken'] = this.prevPageToken;
		data['requestCount'] = this.requestCount;
		data['totalResults'] = this.totalResults;
		data['nextPageToken'] = this.nextPageToken;
		data['responseCount'] = this.responseCount;
		if (this.items != null) {
      data['items'] =  this.items.map((v) => v).toList();
    }
		return data;
	}
}
